package com.example.app.manager

import android.annotation.SuppressLint
import android.app.Activity
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.example.app.R
import com.github.nukc.stateview.StateView

@SuppressLint("NonConstantResourceId")
class StateViewManager private constructor(private val mStateView: StateView) {

    companion object {
        fun inject(view: View): StateViewManager {
            return StateViewManager(StateView.inject(view))
        }

        fun inject(activity: Activity): StateViewManager {
            return StateViewManager(StateView.inject(activity))
        }

        fun inject(viewGroup: ViewGroup): StateViewManager {
            return StateViewManager(StateView.inject(viewGroup))
        }
    }

    init {
        //设置默认的状态页面
        setLoadingResId(R.layout.app_state_view_loading)
        setRetryResId(R.layout.app_state_view_retry)
        setEmptyResId(R.layout.app_state_view_empty)
    }

    fun setLoadingResId(@LayoutRes loadingResource: Int): StateViewManager {
        mStateView.loadingResource = loadingResource
        return this
    }

    fun setEmptyResId(@LayoutRes emptyResource: Int): StateViewManager {
        mStateView.emptyResource = emptyResource
        return this
    }

    fun setRetryResId(@LayoutRes retryResource: Int): StateViewManager {
        mStateView.retryResource = retryResource
        return this
    }

    fun setEmptyView(view: View): StateViewManager {
        mStateView.setView(mStateView.emptyResource, view)
        return this
    }

    fun setRetryView(view: View): StateViewManager {
        mStateView.setView(mStateView.retryResource, view)
        return this
    }

    fun setLoadingView(view: View): StateViewManager {
        mStateView.setView(mStateView.loadingResource, view)
        return this
    }

    fun showEmpty() {
        mStateView.showEmpty()
    }

    fun showLoading() {
        mStateView.showLoading()
    }

    fun showRetry() {
        mStateView.showRetry()
    }

    fun showContent() {
        mStateView.showContent()
    }

    /** 不需要调用showLoading()方法, StateView自会调用 */
    fun setOnRetryClickListener(listener: () -> Unit): StateViewManager {
        mStateView.onRetryClickListener = object : StateView.OnRetryClickListener {
            override fun onRetryClick() {
                listener.invoke()
            }
        }
        return this
    }

}